/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Army.task;

import Army.BuildingAndUnit;


/**
 *
 * @author Huy
 */
public class TaskBuild extends Task {

	public TaskBuild(BuildingAndUnit source,BuildingAndUnit target) {
		super(source,target);
		if(source.getTeam() != target.getTeam())
			isDone = true;
		taskName = "Build";
	}
	
	@Override
	public boolean execute() {
		if(isDone) {
			return true;
		}
		if (!target.isAlive() || target.isCompleted()) {
			source.idle();
			isDone = true;
			return true;
		}
		int dx = Math.abs(target.getPosition().x-source.getPosition().x);
		int dy = Math.abs(target.getPosition().y-source.getPosition().y);
		double d = Math.max(dx, dy);
		source.setTarget(target.getPosition());
		if (d < source.getAttackRange()+target.getSize()/2) {
			source.attack();
			target.build();
		}
		else
		{
			source.run();
		}
		return false;
	}
}
